﻿class Images
{
	private var _canvas:MovieClip; 
	private var _depth:Number = 100;
	private var _imgArr:Array;
	private var _objArr:Array = new Array();
	private var _imgArrLength:Number;
	private var _imgCount:Number = 0;
	private var _imgDepth:Number;
	private var _imgInt:Number;
	private var sCount:Number = 0;
	private var nCount:Number = 1;
	
	public function Images(canvas:MovieClip, imgArr:Array)
	{
		_canvas = canvas;
		_imgArr = imgArr;
		_imgArrLength = imgArr.length;
		_imgDepth = imgArr.length;
		
		for(var i = 0;i<_imgArr.length;i++)
		{
			LoadImages(_imgArr[i]);
		}
		
	}
	
	private function LoadImages(obj:Object):Void
	{
		var container_mc = _canvas.attachMovie("ImageClip_mc", "ImageClip_mc"+newDepth(), newImagedepth());
		var imageLoader = new MovieClipLoader();
		container_mc.id.text = obj.id;
		imageLoader.addListener(this);
		imageLoader.loadClip(obj.path, container_mc.image_mc);
		_imgCount++;
	}
	
	public function onLoadProgress (target:MovieClip, bytesLoaded:Number, bytesTotal:Number):Void 
	{
		var percent:Number = Math.floor( (bytesLoaded*100)/bytesTotal );
		target._parent.preloader.gotoAndStop(percent);
		target._parent.load_txt.text = percent+"%";
		
		
		if (percent == 100) 
		{
			target._parent.load_txt.removeMovieClip();
			target._parent.loaderTxt.removeMovieClip();
			target._parent.preloader.removeMovieClip();
		}
  	}
	
	public function onLoadInit (target:MovieClip):Void 
	{
		//if(_imgCount < _imgArrLength)
		//{
			//LoadImages(_imgArr[_imgCount]);
		//}
		trace(_objArr.length)
		_objArr.push(target._parent);
		if(_objArr.length == 2)
		{
			_imgInt = setInterval(this,"startShow",2000);
		}
	}
	
	private function startShow():Void
	{
		clearInterval(_imgInt);
		_objArr[sCount].alphaTo(0, 1, "linear");
		_objArr[nCount].alphaTo(100, 1, "linear");
		
		if(sCount == _objArr.length-1)
		{
			sCount = 0;
		}
		else
		{
			sCount++;
		}
		if(nCount == _objArr.length-1)
		{
			nCount = 0;
		}
		else
		{
			nCount++;
		}
		_imgInt = setInterval(this,"startShow",2000);
	}
	
	private function newDepth():Number
	{
		return _depth++;
	}
	
	private function newImagedepth():Number
	{
		return _imgDepth--;
	}
}